var app = getApp();
const homeData = require('../../antiqueData/antiqueData.js');
var util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isloading: false,
    valid: '',
    comment: '',
    requestUrl: '',
    antiqueId: '',
    flag: true,
    otherComment: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    //var antiqueId = option.id === "2684120180427135500" ? "2684120180427135613" : option.id;
    var antiqueId = option.id;
    this.data.antiqueId = antiqueId;
    //var detailData = homeData.antiqueData[antiqueId];
    var dataUrl = app.globalData.antiqueBaseUrl + "topicDetails.html";

    this.data.requestUrl = dataUrl;
    util.httpDetail(dataUrl, antiqueId, this.initData);

  },

  initData: function(index){
    
    // var avatar = util.avatarImg(index.user.headImg);
    var avatar = index.user.headImg;
    var subject = index.imgs;
    var showImg = util.showImg(subject);
    var temp = {
      avatar: avatar,
      nickName: index.user.name,
      publishTime: index.times,
      antiqueName: index.type.name,
      antiqueDesc: index.content,
      browserNum: index.views,
      praiseNum: index.like,
      showImg: showImg,
      otherComment: index.topic_comment,
    }
  
    this.setData({
      detailData: temp,
      otherComment: temp.otherComment,
      praiseNum: temp.praiseNum,
      showImg: temp.showImg
    })
  },

  dianzan: function(){
    if(this.data.flag){
      var dianzanUrl = app.globalData.antiqueBaseUrl + 'addLike.html';
      util.httpDianZan(dianzanUrl,this.data.antiqueId);
      var praiseNum = this.data.praiseNum + 1; 
      this.setData({
        praiseNum: praiseNum
      })
      this.data.flag = false;
    }else{
      wx.showToast({
        title: '你已经点赞过了',
        icon: 'success',
        duration: 500
      })
    }
  },

  previewImage: function(e){
    var that = this,
    //获取当前图片的下表
    index = e.currentTarget.dataset.index,
    //数据源
    pictures = that.data.detailData.showImg;
    wx.previewImage({
      current: pictures[index],
      urls: pictures
    })

  },

  more: function(){
    wx.switchTab({
      url: '../index/index'
    })
  },

  onBindComment:function(e){
    var antiqueName = e.currentTarget.dataset.antiquename;
    wx.navigateTo({
      url: "/pages/comment/comment?title=" + antiqueName
    })
  },


  onReady: function () {
    var that = this;
    wx.setStorage({
      key: 'shareTest',
      data: {
        showImg: that.data.showImg,
      }
    })    
  },


  onShow: function(){
    var that = this;
    var otherComment = this.data.otherComment;
    wx.getStorage({
      key: 'comment',
      success: function(res){
        app.getUserInfo(function (userInfo){
          var newComment = {
            commentName: userInfo.nickName,
            commentRemark: res.data.content
          };
          otherComment.unshift(newComment);
          that.setData({
            otherComment: otherComment
          })
        })
      }
    })
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '测眼力',
      path: '/pages/shareTestSight/shareTestSight',
      success: function(res) {
        // 转发成功
        console.log("转发成功", res);
        // wx.navigateTo({
        //   url: '../shareTestSight/shareTestSight'
        // })  
      },
      fail: function(res) {
        // 转发失败
        console.log("转发失败", res);  
      }
    }
  }
})